home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / Snippets / Toolbox & IAC / AppleEvents / LaunchWithDoc / LaunchWithDoc.c next >
Encoding:
C/C++ Source or Header  |  1992-01-01  |  4.0 KB  |  98 lines  |  [TEXT/MPS ]

  1. /* LaunchWithDoc */
  2. /* The smallest LaunchAplication example I could come up with. */
  3. /* this launches an application with a 'odoc' AppleEvent™ */
  4. /* this little bit of code here uses two Standard File calls to get */
  5. /* the application to launch and the file to launch with, you will of course */
  6. /* want to replace these with whatever you want to do */
  7. /* ••• NOTE: This also works for launching non-System 7 applications.  */
  8. /* If the Finder™ sees an 'odoc' or 'pdoc' Apple event in the launch block */
  9. /* and the application being launched is NOT system 7 aware, the Finder */
  10. /* coerces the Apple event into puppetstrings.  So you */
  11. /* don't need to special case for non-7.0 applications.  */
  12. /* Pretty neat, huh? */
  13. /* ••• Important: I'm not doing any error checking, you of course should */
  14. /* so your users don't hunt you down and burn your keyboard. */
  15. /* C.K. Haun */
  16. /* Apple DTS */
  17. #include <Dialogs.h>
  18. #include <QuickDraw.h>
  19. #include <Windows.h>
  20. #include <Menus.h>
  21. #include <Fonts.h>
  22. #include <appleevents.h>
  23. #include <processes.h>
  24. #include <files.h>
  25. #include <StandardFile.h>
  26. #include <Aliases.h>
  27. main()
  28. {
  29.     FSSpec launchApp, fileToLaunch;   /* file spec for the app (launchApp) and the file (fileToLaunch) */
  30.     LaunchParamBlockRec launchThis;
  31.     AEDesc myAddress;
  32.     AEDesc docDesc, launchDesc;
  33.     AEDescList theList;
  34.     AliasHandle withThis;
  35.     StandardFileReply myReply;
  36.     AppleEvent theEvent;
  37.     InitGraf((Ptr)&qd.thePort);  /* standard setup stuff */
  38.     InitFonts();
  39.     InitWindows();
  40.     InitMenus();
  41.     TEInit();
  42.     InitDialogs(nil);
  43.     InitCursor();
  44.     /* get the app to launch, using the Sys7 versions of Standard File */
  45.     /* which return FSSpecs */
  46.     StandardGetFile(nil, -1, nil, &myReply);
  47.     if (!myReply.sfGood)
  48.         return;
  49.     launchApp = myReply.sfFile;
  50.     /* stuff it in my launch parameter block */
  51.     launchThis.launchAppSpec = &launchApp;
  52.     
  53.     /* get the file to pass */
  54.     StandardGetFile(nil, -1, nil, &myReply);
  55.     if (!myReply.sfGood)
  56.         return;
  57.     
  58.     /* the caller may have already done this, but it doesn't hurt to do it again */
  59.     fileToLaunch = myReply.sfFile;
  60.     /* create an appleevent to carry it */
  61.     AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, &myAddress, kAutoGenerateReturnID, kAnyTransactionID, &theEvent);
  62.     /* create a list for the alaises.  In this case, I only have one, but you still need */
  63.     /* a list */
  64.     AECreateList(nil, 0, false, &theList);
  65.     /* create an alias out of the file spec */
  66.     /* I'm not real sure why I did this, since there is a system coercion handler for */
  67.     /* alias to FSSpec, but I'm paranoid */
  68.     NewAlias(nil, &fileToLaunch, &withThis);
  69.     HLock((Handle)withThis);
  70.     /* now create an alias descriptor */
  71.     AECreateDesc(typeAlias, (Ptr)*withThis, GetHandleSize((Handle)withThis), &docDesc);
  72.     HUnlock((Handle)withThis);
  73.     /* put it in the list */
  74.     AEPutDesc(&theList, 0, &docDesc);
  75.     AEPutParamDesc(&theEvent, keyDirectObject, &theList);
  76.     /* coerce the event from being an event into being appParms */
  77.     /* •••• If you just want to send an 'odoc' to an application that is */
  78.     /* already running, you can stop here and do an AESend on theEvent */
  79.     AECoerceDesc(&theEvent, typeAppParameters, &launchDesc);
  80.     HLock((Handle)theEvent.dataHandle);
  81.     /* and stuff it in the parameter block */
  82.     /* This is a little weird, since we're actually moving the event out of the */
  83.     /* AppParameters descriptor.  But it's necessary, the coercison to typeAppParameters */
  84.     /* stuffs the whole appleevent into one AERecord (instead of a AEDesc) so  */
  85.     /* the Finder gets the whole event as one handle.  It can then parse it itself */
  86.     /* to do the sending */
  87.     launchThis.launchAppParameters = (AppParametersPtr)*(launchDesc.dataHandle);
  88.     /* launch the thing */
  89.     launchThis.launchBlockID = extendedBlock;
  90.     launchThis.launchEPBLength = extendedBlockLen;
  91.     launchThis.launchFileFlags = nil;
  92.     launchThis.launchControlFlags = launchContinue + launchNoFileFlags;
  93.     LaunchApplication(&launchThis);
  94.     /* and launch it */
  95.     
  96.     
  97. }
  98.